﻿
var TLM = function (fieldName) {
    if (this == window) {
        return new TLM(fieldName);
    }
    fieldName = fieldName.toLowerCase();
    this.field = EntityField[fieldName];

    this.fieldName = fieldName;
    return this;
}




TLM.prototype = {
    SetValue: function (value) {
        try {
            switch (this.field['Type']) {
                case 'lookup':
                    {
                        if (value == null) {
                            $('#' + this.fieldName).attr('oid','');
                            $('#' + this.fieldName).val('');
                        } else {
                            $('#' + this.fieldName).attr('oid', value['id']);
                            $('#' + this.fieldName).val(value["name"]);
                        }

                        break;
                    }

                default:
                    {
                        $('#' + this.fieldName).val(value==null? '':value);
                    }
            }
            $('#' + this.fieldName).trigger('change');
            return this;
        } catch (e) {

        } 
        
    },
    GetValue: function () {
        try {
            var returnData = {};
            returnData['dataType'] = this.field['Type'];
            returnData["data"] = {};
            switch (this.field['Type']) {
                case 'lookup':
                    returnData["data"] = $('#' + this.fieldName).attr('oid'); break;
                case 'picklist':
                    {
                        if ($('#' + this.fieldName).val().length == 0) return null;
                        returnData["data"]['value'] = Number($('#' + this.fieldName).val());
                        returnData['data']['name'] = $('#' + this.fieldName).find("option:selected").text();
                        break;
                    }
                default:
                    {
                        returnData["data"] = $('#' + this.fieldName).val();
                    }
            }
            return returnData;
        } catch (e) {

        } 
    },
    SetDisable: function (state) {
        try {
            if (typeof (state) != 'boolean') {
                return this;
            }
            $('#' + this.fieldName).attr('disabled', state);
            $('#' + this.fieldName).find('*').attr('disabled', state);
            return this;
        } catch (e) {

        }
    },
    SetReadonly: function (state) {
        try {
            if (typeof (state) != 'boolean') {
                return this;
            }
            this.fieldName = this.fieldName.toLowerCase();
            $('#' + this.fieldName).attr('readonly', state);
            $('#' + this.fieldName).find('*').attr('readonly', state);
            return this;
        } catch (e) {

        }
    },
    IsDirty: function () {
        try {
            switch (this.field['Type']) {
                case 'key':
                    {
                        return false;
                    }
                case 'lookup':
                    {
                        if ((XFormData[this.fieldName] == null ? '' : XFormData[this.fieldName]['id']) == (this.GetValue()['data'] == '' ? '' : this.GetValue()['data']['id'])) return false;
                        else
                            return true;

                    }
                case 'picklist':
                    {
                        if (XFormData[this.fieldName] == null ? '' : XFormData[this.fieldName] == (this.GetValue()["data"]['value'] == '' ? '' : this.GetValue()['data']['value'])) return true;
                        else {
                            return false;
                        }
                    }
                default:
                    {
                        if ((XFormData[this.fieldName] == null ? '' : XFormData[this.fieldName]) == this.GetValue()['data']) {
                            return false;
                        }
                        return true;
                    }
            }
        } catch (e) {

        }
    },
    SetVisible: function (state) {
        try {
            if (typeof (state) != 'boolean') {
                return this;
            }

            if (!state) {
                $('#c_' + this.fieldName).addClass('hidden');
                return;
            }
            $('#c_' + this.fieldName).removeClass('hidden');
            return this;
        } catch (e) {

        }
    },
    SetLookupCustomFunction: function (func) {
        core.Lookup.CustomSelectedFunctionList[this.fieldName] = func;
        return this;
    }
};

TLM.SetAttAddDisable = function (state) {
    if (typeof (state) != 'boolean') {
        return;
    }
    try {
        if (state) {
            $('[attachmenttooltype=AddButton]').addClass('hidden');
        } else {
            $('[attachmenttooltype=AddButton]').removeClass('hidden');
        }
    } catch (e) {

    }
}
TLM.SetAttEditDisable = function (state) {

    try {
        if (typeof (state) != 'boolean') {
            return;
        }
        if (state) {
            $('[attachmenttooltype=EditButton]').addClass('hidden');
        } else {
            $('[attachmenttooltype=EditButton]').removeClass('hidden');
        }


    } catch (e) {

    }
}
TLM.SetAttDeleteDisable = function (state) {
    try {
        if (typeof (state) != 'boolean') {
            return;
        }
        if (state) {
            $('[attachmenttooltype=DeleteButton]').addClass('hidden');
        } else {
            $('[attachmenttooltype=DeleteButton]').removeClass('hidden');
        }

    } catch (e) {

    }
}

TLM.fn = TLM.prototype;